package com.ljt.framework.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

import com.ljt.framework.test.demo.StaticUserService;
import com.ljt.framework.test.demo.UserAction;

/**
 * @Project       : framework-test
 * @Program Name  : com.ljt.framework.test.MockForPublicStaticDemo.java
 * @Description   : 测试含有静态方法的服务
 * @Author        : wangchao
 * @Creation Date : 2016年4月14日 上午11:08:50 
 * @ModificationHistory  
 * Who          When             What 
 * ----------   -------------    -----------------------------------
 * wangchao     2016年4月14日        create
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticUserService.class,UserAction.class})
public class MockForPublicStaticDemo {
	
	@Test
	public void test() throws Exception{
		
		// mock会模拟所有方法
		// PowerMockito.mock(StaticUserService.class); 
		// spy 只会模拟指定模拟行为的方法
		PowerMockito.spy(StaticUserService.class);
		
		// 模拟返回值的public T static 方法
		when(StaticUserService.sayHello(anyString())).thenReturn("mock");
		// 模拟无返回值的public void static 方法
		PowerMockito.doNothing().when(StaticUserService.class,"sayHi",anyString());
		
		// 业务方法调用
		UserAction userAction = new UserAction();
		userAction.executeForPublicStatic("public static");
		
		// 验证
		PowerMockito.verifyStatic(Mockito.times(1));
		StaticUserService.sayHello(anyString());
		
		PowerMockito.verifyStatic(Mockito.times(1));
		StaticUserService.sayHi(anyString());
	}

}
